package com.file.controller.interceptor;

import com.file.sys.SysMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
@Slf4j
public class PathInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        String path = (String) session.getAttribute("path");
        if (null==path){
            SysMsg.path.set(SysMsg.root);
            log.info("root："+SysMsg.root);
            session.setAttribute("path",SysMsg.root);
        }else {
            SysMsg.path.set(path);
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        String p = SysMsg.path.get();
        HttpSession session = request.getSession();
        String path = (String) session.getAttribute("path");
        if (!p.equals(path)){
            session.setAttribute("path",p);
        }
        SysMsg.path.remove();
    }
}
